<?php
	/**
	Exports the class record as a Comma-Separated Values (CSV) file.
	The implementation is largely based on grades.php . Students are
	still sorted by last name.	
	
	[Chad Estioco; Second Semester, AY 2010-2011]
	*/
	header ("Content-type: application/csv");
	header("Content-Disposition: inline; filename=Report.csv");
	//In case this page was generated via an export command.
	
	include ("write_functions.php");
	connect("localhost", "root", "", "gradegriddatabase");
	
	$classid = $_GET["classid"];
	
	//Query for the major requirements
	$getreqs_query = sprintf("SELECT name, major_id FROM major WHERE template_id IN
	                         (SELECT template_id FROM class WHERE class_id = '%s') ORDER BY major_id",
							 $classid);
	$grq = mysql_query($getreqs_query);
	$majors_limit = mysql_num_rows($grq);
	
	//Query for the students
	$findstudents_query = sprintf("SELECT last_name, first_name, account_id FROM account WHERE account_id IN
	                               (SELECT account_id FROM take WHERE class_id = '%s') ORDER BY last_name, first_name",
								   $classid);
	$fsq = mysql_query($findstudents_query);
	$student_limit = mysql_num_rows($fsq);
	
	$i = 0;
	$limit = mysql_num_rows($grq);
	$minlimit = 0;
	$formnames = array();
	$minorids = array();
	echo "Student Names";
	//Echo the student names in the table, alongside the minor requirements.
	//(See above.)
	while($i < $limit){
		mysql_data_seek($grq, $i);
		$requirements = mysql_fetch_array($grq);
		
		//...the minor requirements
		$getminors_query = sprintf("SELECT * FROM minor WHERE major_id = '%s' ORDER BY name, minor_id",
		                           mysql_real_escape_string($requirements["major_id"]));
		$gmq = mysql_query($getminors_query);
		$j = 0;
		$minors_limit = mysql_num_rows($gmq);
		$minlimit += $minors_limit;
			
		while($j < $minors_limit){
			mysql_data_seek($gmq, $j);
			$minors = mysql_fetch_array($gmq);
			echo "," .$requirements["name"] . " " . $minors["name"] . "(" . $minors["maxrawscore"] . ")";
			array_push($formnames, $fieldname); //Take the names for the fields in the form (to be made unique
			                                    //by their matrix coordinates.)
			array_push($minorids, $minors["minor_id"]); //Take all the minorids associated with this record.
			$j++;
		}
		$i++;
	}
	echo ",Total\n";
	$i = 0;
	$limit = mysql_num_rows($fsq);
	
	//Now output the fields for the grade
	while($i < $limit){
		mysql_data_seek($fsq, $i);
		$students = mysql_fetch_array($fsq);
		$accountid = mysql_real_escape_string($students["account_id"]);
		
		//Get the student grades
		$getgrades_query = sprintf("SELECT grade, major_id, minor_id, account_id FROM grade WHERE account_id='%s' and class_id IN
                                       (SELECT class_id FROM class, course WHERE class_id = '%s') ORDER BY major_id, minor_id",
		                           $accountid, $classid);
		$ggq = mysql_query($getgrades_query);
		$j = 0;
		$limit2 = mysql_num_rows($ggq);
		$namecounter = 0;
		$seek = true;
		
		echo "\"" . $students["last_name"] . ", " . $students["first_name"], "\"";
		
		/*
		Get all the available grades for the student. Since not all grades may be available
		for the moment, we still need to check if we are echo-ing under a column with an available
		grade. Note that this scheme relies heavily on the ordering of the queried tuples.
		*/
		while($j < $minlimit){
			if($j < $limit2 && $seek){
				mysql_data_seek($ggq, $j);
			}
			$grades = mysql_fetch_array($ggq);
			$name = $formnames[$namecounter] . $i . $j;
			if($grades["minor_id"] == $minorids[$namecounter]){ //If we are currently in the column for a recorded grade.
				echo "," . $grades["grade"];
				$seek = true;
			} else{
				echo ",";
				$seek = false;
			}
			$j++;
			$namecounter++;
		}
		
		//And another one for the total.
		//Query the student's total if it exists.
		$studtotal_qs = sprintf("SELECT totalgrade FROM total WHERE class_id = '%s' AND account_id = '%s'",
		                        $classid, $accountid);
		$studtotal_select_query = mysql_query($studtotal_qs);
		
		if($studtotal_select_query){
			$studtotal_array = mysql_fetch_array($studtotal_select_query);
			$totalgrade = $studtotal_array["totalgrade"];
			echo ",$totalgrade\n";
		} else{
		}
	
		$i++;
	}
?>